home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
circle.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1985-06-03
|
4KB
|
75 lines
(*****************************************************************************)
(* Plot circle of radius "radius" with origin (x_coor,y_coor) *)
(* (( written by Bill Gaupp 1-Dec-1984 )) *)
(* This method breaks a circle into 8 sectors, each with a 45 degree *)
(* central angle. For each i measured along a perpendicular to one of the *)
(* sides of a sector up to the farthest point still within the sector, a *)
(* corresponding distance j is calculated such that i and j form the sides *)
(* of a right triangle with hypotenuse "radius". Each of these triangles *)
(* can be inscribed in the desired circle with one of the non-right-angle *)
(* points at the center and the other on the circle. Points are plotted an *)
(* offset of (i,j) or (j,i) from the coordinates given for the center of *)
(* the circle for each of the eight sectors. For a rectangular frame *)
(* buffer, these are the best-approximation points for a circle. *)
(* *)
(*****************************************************************************)
PROCEDUR┼ Circle¿ x¼ y¼ radiu≤ ║ INTEGER╗ set_pixel : BOOLEAN;
VAR frame : Frame_Buffer );
VAR
radius_squared : REAL;
i, j : INTEGER;
BEGIN { Circle }
radius_squared := radius * radius;
FOR i := 0 TO ROUND( radius / SQRT(2) ) DO
BEGIN
j := ROUND( SQRT( radius_squared - i*i ) );
Put_Pixel¿ x ½ j¼ y - i¼ set_pixel¼ framσ )╗ √ quadran⌠ 1¼ bottoφ quad }
Put_Pixel( x + i, y - j, set_pixel, frame ); { quadrant 1, top quad }
Put_Pixel( x - i, y - j, set_pixel, frame ); { quadrant 2, top quad }
Put_Pixel( x - j, y - i, set_pixel, frame ); { quadrant 2, bottom quad }
Put_Pixel( x - j, y + i, set_pixel, frame ); { quadrant 3, top quad }
Put_Pixel( x - i, y + j, set_pixel, frame ); { quadrant 3, bottom quad }
Put_Pixel( x + i, y + j, set_pixel, frame ); { quadrant 4, bottom quad }
Put_Pixel¿ ° ½ j¼ y ½ i¼ set_pixel¼ framσ )╗ √ quadran⌠ 4¼ top quad }
END; { for }
END; { Circle }
(*****************************************************************************)
(* Draw a line from point (x1,y1) to point (x2,y2) *)
(* (( written by Bill Gaupp 1-Dec-1984 )) *)
(* Method used is to move from point (x1,y1) to (x2,y2) in n steps, *)
(* where n equals the length of the longest projection of the line onto *)
(* the two axis. This method will visit every pixel between (x1,y1) and *)
(* (x2,y2) on a rectangular frame buffer. Trivial (fast) case of *)
(* adjacent pixels is handled first for speed. *)
(* *)
(*****************************************************************************)
PROCEDUR┼ Line¿ x1¼ y1¼ x2¼ y▓ ║ INTEGER╗ set_pixe∞ ║ BOOLEAN;
VAR frame : Frame_Buffer );
VAR
length, i : INTEGER;
x_step, y_step, x, y : REAL;
BEGIN
IF ( ABS(x1 - x2) <= 1 ) AND ( ABS(y1 - y2) <= 1 )
THEN
BEGIN { adjacent pixels }
Put_Pixel( x1, y1, set_pixel, frame );
Put_Pixel( x2, y2, set_pixel, frame );
END
ELSE
BEGIN
IF ABS(x2 - x1) > ABS(y2 - y1)
THEN length := ABS(x2 - x1) { horizontal sweep }
ELSE length := ABS(y2 - y1); { vertical sweep }
x := x1; x_step := (x2 - x1) / length; { initial and }
y := y1; y_step := (y2 - y1) / length; { increment size }
FOR i := 0 TO length DO
BEGIN
Put_Pixel( ROUND(x), ROUND(y), set_pixel, frame );
x := x + x_step;
y := y + y_step;
END; { for }
END; { if }
END; { Line }